package dao.Impl;

import commons.druidJdbcUtil;
import dao.SelectUserDao;
import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SelectUserImpl implements SelectUserDao {
    @Override
    public List<User> selectUser(User user) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<User> list=new ArrayList<>();
        try {
            conn=druidJdbcUtil.getConnection();
            ps=conn.prepareStatement(this.joinSql(user));
            rs=ps.executeQuery();
            while(rs.next()){
                User user1=new User();
                user1.setUserid(rs.getInt("userid"));
                user1.setPhoneNum(rs.getString("phoneNum"));
                user1.setQqNum(rs.getString("qqNum"));
                user1.setUsername(rs.getString("username"));
                user1.setUsersex(rs.getString("usersex"));
                user1.setUserpwd(rs.getString("userpwd"));
                list.add(user1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            druidJdbcUtil.closeResource(conn,ps,rs);
        }


        return list;
    }

    @Override
    public User selectUserById(int id) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        User user=null;
        try {
            conn=druidJdbcUtil.getConnection();
            ps=conn.prepareStatement("select * from users where userid=?");
            ps.setInt(1,id);
            rs=ps.executeQuery();

            while(rs.next()){
                user=new User();
                user.setUserid(rs.getInt("userid"));
                user.setUserpwd(rs.getString("userpwd"));
                user.setUsersex(rs.getString("usersex"));
                user.setUsername(rs.getString("username"));
                user.setPhoneNum(rs.getString("phoneNum"));
                user.setQqNum(rs.getString("qqNum"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            druidJdbcUtil.closeResource(conn,ps,rs);
        }
        return user;
    }

    public String joinSql(User user){
        StringBuffer stringBuffer=new StringBuffer("select * from users where 1=1 ");
        if(user.getUsername() != null && user.getUsername().length()>0){
            stringBuffer.append(" and username= '"+user.getUsername()+"'");
        }
        if(user.getPhoneNum() != null && user.getPhoneNum().length()>0){
            stringBuffer.append(" and phoneNum= '"+user.getPhoneNum()+"'");
        }
        if(user.getUserpwd() != null && user.getUserpwd().length()>0){
            stringBuffer.append(" and userpwd= '"+user.getUserpwd()+"'");
        }
        if(user.getQqNum() != null && user.getQqNum().length()>0){
            stringBuffer.append(" and qqNum= '"+user.getQqNum()+"'");
        }
        if(user.getUsersex() != null && user.getUsersex().length()>0){
            stringBuffer.append(" and usersex= '"+user.getUsersex()+"'");
        }
        return stringBuffer.toString();
    }
}
